package com.example.bbfindhouse.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bbfindhouse.R;
import com.example.bbfindhouse.Activity.zuyueActivity;

import java.util.List;

public class LeaseItemAdapter extends BaseAdapter {

    private static final String TAG = "LeaseItemAdapter";
    private Context context;
    private int layoutResource;
    private List<zuyueActivity.LeaseItem> leaseItemList;

    public LeaseItemAdapter(Context context, int layoutResource, List<zuyueActivity.LeaseItem> leaseItemList) {
        this.context = context;
        this.layoutResource = layoutResource;
        this.leaseItemList = leaseItemList;
    }

    @Override
    public int getCount() {
        return leaseItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return leaseItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResource, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.houseImage = convertView.findViewById(R.id.house_image);
            viewHolder.titleTextView = convertView.findViewById(R.id.title_text);
            viewHolder.locationTextView = convertView.findViewById(R.id.location_text);
            viewHolder.statusTextView = convertView.findViewById(R.id.status_text);
            viewHolder.timeTextView = convertView.findViewById(R.id.time_text);
            viewHolder.priceTextView = convertView.findViewById(R.id.price_text);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        zuyueActivity.LeaseItem item = leaseItemList.get(position);
        setImageResource(viewHolder.houseImage, item.getImageRes());
        viewHolder.titleTextView.setText(item.getTitle());
        viewHolder.locationTextView.setText(item.getLocation());
        viewHolder.statusTextView.setText(item.getStatus());
        viewHolder.timeTextView.setText(item.getTime());
        viewHolder.priceTextView.setText(item.getPrice());

        return convertView;
    }

    private void setImageResource(ImageView imageView, String imageResName) {
        int imageResId = context.getResources().getIdentifier(imageResName, "drawable", context.getPackageName());
        if (imageResId != 0) {
            imageView.setImageResource(imageResId);
        } else {
            imageView.setImageResource(R.drawable.ic_house1); // 默认图片
        }
    }

    private static class ViewHolder {
        ImageView houseImage;
        TextView titleTextView;
        TextView locationTextView;
        TextView statusTextView;
        TextView timeTextView;
        TextView priceTextView;
    }
}